// components/communityList/communityList.js
const app = getApp();
import LoginStore from '../../pages/login/store.js';
import { proxy, reqHeader } from '../../utils/util.js';
let getDefaultData = function () {
  return {
    listData: [],
    hasNext: true,
    auth_url: '',
    auth_blue: '../../icon/auth_blue.png',
    auth_green: '../../icon/auth_green.png',
    auth_red: '../../icon/auth_red.png',
    auth_black: '../../icon/auth_black.png',
    loginUser: LoginStore.userInfo,
    confirmModalShow: false,
    actionTribe: {},
  }
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    urlStr: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {

      }
    },
    params: {
      type: Object,
      value: {},
      observer: function (newVal, oldVal, changedPath) {
        if (newVal.page == 1) {
          this.setData(getDefaultData())
        }
        if (newVal.page && this.data.hasNext) {
          this._loadData(newVal);
        }
      }
    },
    totalPageUrl: {
      type: String,
      value: null
    },
    type: {
      type: String,
      value: 'large'
    }
  },

  /**
   * 组件的初始数据
   */
  data: getDefaultData(),

  /**
   * 组件的方法列表
   */
  methods: {
    _loadData: function (params) {
      let me = this;
      let userAccountInfo = LoginStore.userInfo || {};
      params['access_token'] = userAccountInfo.access_token;
      wx.request({
        url: this.properties.urlStr,
        data: params,
        header: reqHeader,
        success: function (res) {
          res = res.data;
          if (res.status == 200) {
            let hasNext = true;
            if (!res.data.length || res.data.length < (params.size || 20)) {
              hasNext = false;
            }
            res.data.forEach((item) => {
              if (item.authentication == "OFFICIAL" || item.authentication == "BLUE") {
                item.auth_url = me.data.auth_blue
              } else if (item.authentication == "RED") {
                item.auth_url = me.data.auth_red
              } else if (item.authentication == "GREEN") {
                item.auth_url = me.data.auth_green
              } else if (item.authentication == "BLACK") {
                item.auth_url = me.data.auth_black
              } else if (item.authentication == "NOT_AUTHENTICATION") {
                item.auth_url = null
              }
            })
            let listData = me.data.listData.concat(res.data);
            me.setData({
              listData: listData,
              hasNext: hasNext
            })
          } else {
            console.log(res.message);
          }
        }
      });
    },
    toDetail: function (e) {
      let item = e.currentTarget.dataset.item;

      let urlStr = `/pages/community/index?tribeId=${item.id}`;
      let pageList = getCurrentPages();
      if (pageList.length >= 8) {
        wx.redirectTo({
          url: urlStr,
        })
      } else {
        wx.navigateTo({
          url: urlStr
        });
      }
    },
    showConfirmModal: function (e) {
      let tribe = e.currentTarget.dataset.tribe;
      this.setData({
        confirmModalShow: true,
        actionTribe: tribe
      })
    },
    hideConfirmModal: function () {
      this.setData({
        confirmModalShow: false,
        actionTribe: {}
      })
    },
    toppingTribe: function () {
      let tribe = this.data.actionTribe;
      let urlStr = `${proxy}/community/tribe/v3/topMyTribe?tribeId=${tribe.id}`
      let userAccountInfo = LoginStore.userInfo || {};
      let me = this;
      wx.request({
        url: urlStr,
        data: {
          access_token: userAccountInfo.access_token || '',
        },
        header: reqHeader,
        success: function (res) {
          res = res.data;
          if (res.status == 200) {
            me.triggerEvent('refresh')
          } else {
            console.log(res.message);
          }
        }
      })
    }
  }
})

